<?php 
/*****************
* 服务器管理
*创建日期:2012-8-22
*作者：tjx
******************/
define('IN_WONDERSHARE', true);
header("Content-type:text/html;charset=utf-8");
ob_start("ob_gzhandler");
require('../includes/init.php');
require ('./configs/site_config.php');

class TaskServer {
	
	private  $db;
	private  $table;
	
	/**
	* @ 构造函数
	* @ __construct
	*/
	
	public function __construct() {
		$this->db = $GLOBALS['db'];
		$this->table = 'task_server';
		
	}


	/**
	* 查看服务器列表
	* @param   string      		 $q_user_name      用户名称
	* @return  array
	*/
	public function index() {
		
		$site_rows = array();
		$page = 1;
		if ( !empty($_REQUEST['page']) ) {
			$page = (int)$_REQUEST['page'];
		}
		$pagesize = 10;
		
		$sql = "SELECT COUNT(*) AS total 
				FROM {$this->table}  
				WHERE  serIsDelete = 0  ";
		$page_info = get_page_info($sql, $pagesize, $page);
		$page_url = get_page_url($page, $page_info['page_num']);
		
		$sql = "SELECT 
				serId,
				serName,
				serIp,
				serUpdateUser, 
				serUpdateTime
				FROM {$this->table} 
				WHERE serIsDelete = 0 
				ORDER BY serId ASC
				LIMIT {$page_info['start']}, {$pagesize}";
		$result = $this->db->query($sql);
		while ($row = $this->db->fetch_array($result, MYSQL_ASSOC)) {
			$row['serUpdateTime'] = date('Y-m-d H:i:s', $row['serUpdateTime']);
			$row['serIp'] = long2ip($row['serIp']);
			$server_rows[] = $row;
			
		}
		

		$GLOBALS['smarty']->assign('page_url', $page_url);
		$GLOBALS['smarty']->assign('server_rows', $server_rows);
		$GLOBALS['smarty']->assign('total', $page_info['total']);
		$GLOBALS['smarty']->display('task_server_list.htm');  

		
	}
	
	/**
	* 修改或增加站点
	* @param   string      		 $q_user_name      用户名称
	* @return  array
	*/
	public function editData() {
		
		$site_rows = array();
		$serId = (int) $_GET['serId'];
		if ( !empty($serId) ) {// 编辑
			//队列处理
			queueInfo($serId);	
			
			$sql = "SELECT 
					serId,
					serName,
					serIp
					FROM {$this->table} 
					WHERE serId = {$serId} AND serIsDelete = 0 LIMIT 1";
			$server_row = $this->db->getOneRs($sql);
			$server_row['serIp'] = long2ip($server_row['serIp']);
			
			$GLOBALS['smarty']->assign('form_act', 'edit');
			$GLOBALS['smarty']->assign('form_value', '编辑');
			
		} else {//增加
			$GLOBALS['smarty']->assign('form_act',     'add');
			$GLOBALS['smarty']->assign('form_value',     '增加');
		}
		
		$GLOBALS['smarty']->assign('server_row', $server_row);
		$GLOBALS['smarty']->display('task_server_info.htm');  

		
	}
	
	/**
	* 提交操作
	* @param   array      	$data
	* @return  string
	*/
	public function editSubmit() {
		
		//验证数据合法性
		if ( empty($_POST['serName']) || empty($_POST['serIP']) ) {
			
			echo '服务器名称或服务器IP不能为空！';
			exit;
		}
		
		//封装数据
		$serId = (int) $_POST['serId'];
		$data = array(	'serName' => trim($_POST['serName']),
						'serIP' => ip2long(trim($_POST['serIP'])),
						'serUpdateTime' => $_SERVER['REQUEST_TIME'],
						'serUpdateUser' => $_SESSION['AName'],
					);
					
		//提交表单
		if( $_POST['act'] == 'add' && empty($serId) ) { //增加站点操作

			$data['serCreatTime'] = $_SERVER['REQUEST_TIME'];
			$data['serCreatUser'] = $_SESSION['AName'];
			//增加站点
			$sId = $this->db->insert($this->table, $data);
			if ( !empty($sId) ) {
				
				$location_url = "/wondershare/task_server.php";
				$alert_title = '增加成功！';
				
			} else {
				$location_url = "/wondershare/task_server.php?action=editData";
				$alert_title = '增加失败！';
			
			}
			
			mainAlert($alert_title, $location_url);
			
		} elseif ( $_POST['act'] == 'edit' && !empty($serId) ) {//修改站点操作
			//删除队列
			queueDelete(0, $serId);
			//返回影响的行数
			$lines = $this->db->update($this->table, $data, " serId = {$serId} AND serIsDelete = 0  ");
			if ( $lines > 0 ) {
				
				$location_url = "/wondershare/task_server.php";
				$alert_title = '修改成功！';
				
			} else {
				$location_url = "/wondershare/task_server.php?action=editData&serId={$serId}";
				$alert_title = '修改失败！';
			
			}
			mainAlert($alert_title, $location_url);
			
		}
	}
	
	/**
	* 验证数据
	* @param   array      	$data
	* @return  string
	*/
	public function checkDate($data) {
		
		if (empty($data['sName']) || empty($data['sDomainName'])) {
			
			echo '站点名称或站点域名不能为空！';
			exit;
		}
	}
	
	/**
	* 删除队列记录
	* @param   int      		$qid       队列主键
	* @param   int        	 	 $id       编辑ID
	* @return  array
	*/
	public function delete() {
		
		$str_ser_id = trim($_GET['str_ser_id']);
		if ( !empty($str_ser_id) ) {
			
			$data = array(	'serUpdateTime' => $_SERVER['REQUEST_TIME'],
							'serUpdateUser' => $_SESSION['AName'],
							'serIsDelete' => 1
						);
						
			$where_str = " FIND_IN_SET(serId, '{$str_ser_id}') ";
			$rows_affected = $this->db->update($this->table, $data, $where_str);	
			$str = $rows_affected >= 1 ? '删除成功' : '删除失败'; 
			echo $str;
		}	
	}
}

$action    = $_REQUEST['action']; 
$task_server   = new TaskServer;
if ( method_exists($task_server, $action) )  {
	$task_server->$action();
} else {
	$task_server->index();
}

?>
